<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/24
 * Time: 17:22
 */
namespace app\api\model;

class Product extends BaseModel {

    protected $hidden = ['from','pivot','category_id','delete_time','create_time','update_time'];

    /**
     * tp5获取器，修改product表中的main_img_url的图片url参数
     * @param $value
     * @param $data
     * @return string
     */
    public function getMainImgUrlAttr($value,$data){
        return $this->prefixImgUrl($value,$data);
    }

    /**product表关联product_image表  一对多的关系
     * @return \think\model\relation\HasMany
     */
    public function productImgs(){
        return $this->hasMany('ProductImage','product_id','id');
    }

    /**product表关联product_property表  一对多的关系
     * @return \think\model\relation\HasMany
     */
    public function property(){
        return $this->hasMany('ProductProperty','product_id','id');
    }

    public static function getMostNewProducts($count){
        return self::limit($count)->order('create_time desc')->select();
    }

    public static function getCateProductsByID($id){
        return self::where('category_id','=',$id)->select();
    }

    public static function getProductInfoByID($id){
//        return self::with(['productImgs.imgUrl','property'])->find($id);
        return self::with([
                'productImgs' => function($query){
                $query->with(['imgUrl'])->order('order');
            }
        ])->with(['property'])->find($id);
    }
}